#!/usr/bin/env sh
#==============HEADER===============================================|
#AUTHOR
#  Jefferson Rocha <lrcjefferson@gmail.com>
#
#PROGRAM
#  heyslack - Utilities install for Slackware
#
#DESCRIPTION
#  
#
#SUPPORT:
#  UPDATE system (update, upgrade-all)
#  LAMP (httpd, mysql, php)
#
#CHANGELOG
#===================================================================|

#==========VARIABLES==================|
version="heyslack 0.1"
##Colors
bold=$(tput bold)
fg_red=$(tput setaf 1)
end=$(tput sgr0)
#=====================================|

#==========TESTES=====================|
# dialog exist?
[ ! $(type -p "dialog") ] && {
    printf '%s\n' "${bold}${fg_red}Need a dialog...${end}"
    exit 1
}

[ "$UID" -ne "0" ] && { 
    dialog --title "ERROR!" --infobox "NEED ROOT!" 0 0
    exit 1
}
#=====================================|


#==========FUNCTIONS==================|

_USAGE(){  # How use heyslack?
text=$(cat <<EOF
ashduhuasdhusadhudsauhdsauhhusadhudsahudsahudashudshau
sadudasuhdsahdsauhdsa
dsasdahudsuhahudsa
EOF
)
   dialog --backtitle "$version" --title "USAGE" \
   --msgbox "$text" 0 0
}


search_install(){ # Search and Install!
   set -e # Error? stop;
   for need in "${LIST[@]}"; {
      if ! type -p "$need" 1>/dev/null 2>&1; then
          dialog --sleep 1                     \
          --title "NOT INSTALLED ON SYSTEM"    \
          --infobox "'${need}' Installing..."  3 35
          slackpkg install "$need"
       else
          dialog --sleep 1                  \
          --title "ALREADY IN THE SYSTEM"   \
          --infobox "${need} OK..." 3 30 
       fi
   }
}

update(){
   slackpkg update && slackpkg upgrade-all
}


lamp(){ # Lamp install
   LIST=("httpd" "mysql" "php")    # LIST FOR SEARCH.
   dialog --infobox "Wait..." 0 0
   search_install # Call Function
}
#=====================================|


# MAIN
while true; do
   menu=$(dialog --stdout                            \
          --backtitle "$version"                     \
          --title "SLACKWARE UTILITIES"              \
          --cancel-label "Sair"                      \
          --ok-label "Instalar"                      \
          --radiolist "Utilities for Slackware"      \
          0 0 0                                      \
          "Update" "update system now" off           \
          "Lamp"   "httpd, mysql, php" off           )
   mode="$?"
   [ "$mode" = "1" ] && exit 0
   case "$menu" in
        Update) update ;;
        Lamp)   lamp   ;;
        *)      _USAGE ;;
   esac
done
